﻿


using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebPartPages;

using System.Text;


using System.Web;
using System.Web.UI.WebControls;

using System.Globalization;



using System.Diagnostics;
using System.Diagnostics.Eventing;

namespace ProtocolsFeature.Layouts.ProtocolsFeature
{
    public partial class PrintReceipt : LayoutsPageBase
    {

        EventLog LogToSystem = new EventLog();


        protected void Page_Load(object sender, EventArgs e)
        {


            if (!IsPostBack)
            {

                InitializeDocumentStatus();



                LocalizeForm();

            }

        }


        protected void InitializeDocumentStatus()
        {


            if (Request.QueryString["List"] != null && Request.QueryString["Item"] != null)
            {


                SPListItem item = SPContext.Current.Web.Lists[new Guid(Request.QueryString["List"])].GetItemById(int.Parse(Request.QueryString["Item"]));

                if (item.Name != null)
                {
                    labelNameValue.Text = item.Name;
                }

               

                if (item[item.ParentList.Fields.GetFieldByInternalName("DocumentSubject").Id] != null)
                {
                    labelSubjectValue.Text = (string)item[item.ParentList.Fields.GetFieldByInternalName("DocumentSubject").Id];

                }


                if (item[item.ParentList.Fields.GetFieldByInternalName("ProtocolID").Id] != null)
                {
                    labelProtocolIDValue.Text = (string)item[item.ParentList.Fields.GetFieldByInternalName("ProtocolID").Id];

                }



                if (item[item.ParentList.Fields.GetFieldByInternalName("CloseDate").Id] != null)
                { 

                    DateTime closeDate = (DateTime)item[item.ParentList.Fields.GetFieldByInternalName("CloseDate").Id];
                    labelApprovalDateValue.Text = closeDate.ToString("dd/MM/yyyy");

                }


                try
                {

                    labelCategoryValue.Text = GetLookupPropertyValue(item, "DocumentCategory");

                }
                catch { ;}



                if (item[item.ParentList.Fields.GetFieldByInternalName("ProtocolDate").Id] != null)
                {

                    DateTime pDate = (DateTime)item[item.ParentList.Fields.GetFieldByInternalName("ProtocolDate").Id];
                    labelProtocolDateValue.Text = pDate.ToString("dd/MM/yyyy");


                }



                try
                {
                    if (item[item.ParentList.Fields.GetFieldByInternalName("DocumentSentBy").Id] != null)
                    {
                        labelSentByValue.Text = item[item.ParentList.Fields.GetFieldByInternalName("DocumentSentBy").Id].ToString();
                        labelSentByValue.Text = labelSentByValue.Text.Substring(labelSentByValue.Text.IndexOf("#") + 1);


                    }
                }
                catch (Exception codeException)
                {
                    WriteError(codeException);
                }


            }



        }




        private string GetLookupPropertyValue(SPListItem item, string fieldName)
        {
            string value = "";

            try
            {




                if (item[item.ParentList.Fields.GetFieldByInternalName(fieldName).Id] != null)
                {
                    string lookupFieldValue = (string)item[item.ParentList.Fields.GetFieldByInternalName(fieldName).Id];

                    if (lookupFieldValue.Length > 0)
                    {
                        value = lookupFieldValue.Substring(lookupFieldValue.IndexOf("#") + 1);
                    }
                }
            }
            catch (Exception codeException)
            {
                WriteError(codeException);
            }


            return value;
        }


        protected void LocalizeForm()
        {



            localPrint.Text = Resource1.PrintReceiptForm_Print;

            labelApprovalDate.Text = Resource1.PrintReceiptForm_ApprovalDate;
            labelName.Text = Resource1.PrintReceiptForm_Name;
            labelSubject.Text = Resource1.PrintReceiptForm_Subject;
            labelProtocolID.Text = Resource1.PrintReceiptForm_ProtocolID;
            labelCategory.Text = Resource1.PrintReceiptForm_Category;
            labelProtocolDate.Text = Resource1.PrintReceiptForm_ProtocolDate;
            labelDeliveryMethod.Text = Resource1.PrintReceiptForm_DeliveryType;
            labelSentBy.Text = Resource1.PrintReceiptForm_SentBy;


        }


        private void WriteError(Exception ex)
        {
            LogToSystem.Source = "SharePoint 2010 Protocols Library";
            LogToSystem.WriteEntry(ex.Message + " : " + ex.StackTrace, EventLogEntryType.Error);

        }
    
       
    }
}
